#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>


QString first;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton_0, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_1, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_2, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_3, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_4, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_5, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_6, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_7, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_8, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_9, SIGNAL(released()),this,SLOT(digit()));
    connect(ui->pushButton_clear, SIGNAL(released()),this,SLOT(clear()));
    connect(ui->pushButton_Backspace, SIGNAL(released()),this,SLOT(backspace()));
    connect(ui->pushButton_unar, SIGNAL(released()),this,SLOT(change()));
    connect(ui->pushButton_point, SIGNAL(released()),this,SLOT(point()));
    connect(ui->pushButton_plus, SIGNAL(released()),this,SLOT(operation()));
    connect(ui->pushButton_minus, SIGNAL(released()),this,SLOT(operation()));
    connect(ui->pushButton_mult, SIGNAL(released()),this,SLOT(operation()));
    connect(ui->pushButton_div, SIGNAL(released()),this,SLOT(operation()));
    ui->pushButton_div->setCheckable(true);
    ui->pushButton_plus->setCheckable(true);
    ui->pushButton_minus->setCheckable(true);
    ui->pushButton_mult->setCheckable(true);
    ui->pushButton_equality->setCheckable(true);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::digit(){
    if (ui->display->text().length() == 1 && (ui->display->text().contains("*") || ui->display->text().contains("/")
            || ui->display->text().contains("+") || ui->display->text().contains("-"))) {
        ui->display->clear();
    }
    if (ui->pushButton_equality->isChecked()) {
        ui->display->setText("");
        first = "";
        ui->pushButton_equality->setChecked(false);
    }
    QPushButton *button = (QPushButton*) sender();
    ui->display->setText(ui->display->text() + button->text());
}

void MainWindow::backspace(){
    QString text = ui->display->text();
    text.chop(1);
    ui->display->setText(text);
}

void MainWindow::clear() {
    ui->display->clear();
}

void MainWindow::change(){
    QString text = ui->display->text();
    double val = text.toDouble();

    if (val > 0.0) {
        text.prepend("-");
    } else if (val < 0.0) {
        text.remove(0, 1);
    }
    ui->display->setText(text);
}

void MainWindow::point() {
    if (!(ui->display->text().contains("."))) {
        ui->display->setText(ui->display->text() + ".");
    }
}

void MainWindow:: operation() {
    QPushButton *button = (QPushButton*)sender();
    if (ui->pushButton_div->isChecked()) {
        ui->pushButton_div->setChecked(false);
    } else if (ui->pushButton_plus->isChecked()) {
        ui->pushButton_plus->setChecked(false);
    } else  if (ui->pushButton_minus->isChecked()) {
        ui->pushButton_minus->setChecked(false);
    } else if (ui->pushButton_mult->isChecked()) {
        ui->pushButton_mult->setChecked(false);
    } else if (ui->pushButton_equality->isChecked()) {
        ui->pushButton_equality->setChecked(false);
    }
    first = ui->display->text();
    ui->display->setText(button->text());
    button->setChecked(true);
}



void MainWindow::on_pushButton_equality_released()
{
    if (ui->pushButton_plus->isChecked()) {
        first = QString::number(first.toDouble() + ui->display->text().toDouble());
        ui->display->setText(first);
        ui->pushButton_plus->setChecked(false);
        ui->pushButton_equality->setChecked(true);
    } else if (ui->pushButton_minus->isChecked()) {
        first = QString::number(first.toDouble() - ui->display->text().toDouble());
        ui->display->setText(first);
        ui->pushButton_minus->setChecked(false);
        ui->pushButton_equality->setChecked(true);
    } else if (ui->pushButton_div->isChecked()) {
        if (ui->display->text().toDouble() == 0.0) {
            return;
        }
        first = QString::number(first.toDouble() / ui->display->text().toDouble());
        ui->display->setText(first);
        ui->pushButton_div->setChecked(false);
        ui->pushButton_equality->setChecked(true);
    } else if (ui->pushButton_mult->isChecked()) {
        first = QString::number(first.toDouble() * ui->display->text().toDouble());
        ui->display->setText(first);
        ui->pushButton_mult->setChecked(false);
        ui->pushButton_equality->setChecked(true);
    }
}


void MainWindow::on_pushButton_clearall_released()
{
    ui->pushButton_div->setChecked(false);
    ui->pushButton_plus->setChecked(false);
    ui->pushButton_minus->setChecked(false);
    ui->pushButton_mult->setChecked(false);
    ui->pushButton_equality->setChecked(false);
    first.clear();
    ui->display->setText("");
}

